package com.lyon.custom.core;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.lyon.custom.common.agent.HttpAgent;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author LeeYan9
 * @since 2021-12-02
 */
@Slf4j
public class ServerHttpAgent implements HttpAgent {

    @Override
    public HttpRequest get(String url, Map map, int timeout) {
        return HttpRequest.get(url).form(map).timeout(timeout);
    }

    @Override
    public String doGet(String url, Map map, int timeout) {
        String requestId = IdUtil.fastSimpleUUID();
        log.info("请求：{}:{}", url, JSONUtil.toJsonStr(map));
        String result = get(url, map, timeout).execute().body();
        log.debug("返回数据:{}-{}", requestId, result);
        return result;
    }

    @Override
    public String doPost(String url, String body, int timeout) {
        String requestId = IdUtil.fastSimpleUUID();
        log.info("请求：{}-{}:{}", requestId, url, body);
        String result = post(url, body, timeout).execute().body();
        log.debug("返回数据:{}-{}", requestId, result);
        return result;
    }

    @Override
    public HttpRequest post(String url, String body, int timeout) {
        return HttpRequest.post(url).body(body).timeout(timeout);
    }
}
